﻿using System;
using System.Collections.Generic;
using System.Dynamic;

namespace _140_自定义的动态类型_动态类型
{
    class Program
    {
        static void Main(string[] args)
        {
            dynamic dv = new 自定义类();

            dv.ItemA = 3000;
            dv.ItemB = (uint)50000;
            dv.ItemC = 'p';

            Console.WriteLine($"ItemA:{dv.ItemA},{dv.ItemA.GetType()}");
            Console.WriteLine($"ItemB:{dv.ItemA},{dv.ItemB.GetType()}");
            Console.WriteLine($"ItemC:{dv.ItemA},{dv.ItemC.GetType()}");
                

        }
    }

    public class 自定义类 : DynamicObject
    {

        private IDictionary<string, object> _data = new Dictionary<string, object>();
        //重写TryGetMember方法
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            return _data.TryAdd(binder.Name.ToLower(), value);
        }

        //重写TrySetMember方法
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return _data.TryGetValue(binder.Name.ToLower(), out result);
        }

    }



}
